package com.cdyx.mongo.mangoproject;

import com.cdyx.mongo.mangoproject.entity.User;
import com.cdyx.mongo.mangoproject.repository.UserRepository;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

@SpringBootTest
class MangoprojectApplicationTest1 {
    @Autowired
    private UserRepository userRepository;

    @Test
    void mongoInsert(){
        /**
         * 添加操作主要是调用insert方法
         */
        User user=new User();
        user.setName("蒋东琼");
        user.setEmail("184789416@qq.com");
        user.setAge(21);
        User save = userRepository.save(user);
        System.out.println(save);

    }
    @Test
    void  mongoQuery(){
        /**
         * 查询所有数据
         */
        List<User> all = userRepository.findAll();
        System.out.println(all);

    }
    @Test
    void mongoQueryById(){
        /**
         * 根据id查询
         */

        User user = userRepository.findById("60ae51ac255f2114aea5595f").get();
        System.out.println(user);

    }
    @Test
    void mongoQueryCondition(){
        //条件查询  age=21
        User user=new User();
        user.setAge(21);
        Example<User> userExample=Example.of(user);
        List<User> all = userRepository.findAll(userExample);
        System.out.println(all);
    }
    @Test
    void findUsersLikeName(){
        /**
         * 模糊查询
         * compile编译
         */
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        User user=new User();
        user.setName("j");
        Example<User> example = Example.of(user, matcher);
        List<User> all = userRepository.findAll(example);
        System.out.println(all);
    }
    @Test
     void testPageQuery(){
        /**
         * 分页查询
         */
        User user=new User();
       //设置分页参数 0代表的是第一页
        Pageable page = PageRequest.of(0, 2);
        user.setName("jdq");
        Example<User> of = Example.of(user);
        Page<User> all = userRepository.findAll(of, page);
        System.out.println(all);

    }
    /**
     * 修改
     *
     */
    @Test
    void updateUser(){
        //根据id查询
        User user = userRepository.findById("6077e25bb72f583c20c7efa3").get();
        user.setName("露西");
        user.setAge(54);
        user.setEmail("11111@qq.com");
        User save = userRepository.save(user);
        System.out.println(save);
    }
    /**
     * 删除
     */
    @Test
    void removeUser(){
        userRepository.deleteById("6077e25bb72f583c20c7efa3");
    }
}
